package com.netdisk.controller;

/**
 * 企业用户注册action
 * @author chenlong
 * 2013-5-16
 */
import static com.netdisk.util.RemoteAddress.getRemoteAddress;
import static com.netdisk.util.UUIDCreator.getUuId;
import static com.netdisk.util.SystemParametersReader.getEntrpriseUserMaxCapacity;
import static com.netdisk.util.SystemParametersReader.getSaveDir;

import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.netdisk.dao.impl.FileSystemDao;
import com.netdisk.entity.Capacity;
import com.netdisk.entity.Company;
import com.netdisk.entity.FileSystem;
import com.netdisk.entity.Group;
import com.netdisk.entity.GroupsUsers;
import com.netdisk.entity.Log;
import com.netdisk.entity.User;
import com.netdisk.service.impl.CompanyService;
import com.netdisk.util.EncrypSHA;
import com.netdisk.vo.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("enterpriseUserRegisterAction")
@Scope("prototype")
public class EnterpriseUserRegisterAction extends ActionSupport  implements ModelDriven{
	
	private UserInfo userInfo = new UserInfo();
	
	private CompanyService companyService;
	
	private FileSystemDao fileSystemDao;
	
	@Resource(name="fileSystemDao")
	public void setFileSystemDao(FileSystemDao fileSystemDao) {
		this.fileSystemDao = fileSystemDao;
	}

	@Resource(name="companyService")
	public void setUserService(CompanyService companyService) {
		System.out.println(companyService);
		this.companyService = companyService;
	}
	
	@Override
	public UserInfo getModel() {
		return userInfo;
	}
	
	@Override
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession	session =  request.getSession();
		if(null == session.getAttribute("company")){
			return INPUT;
		}
		Company company = (Company)session.getAttribute("company"); 
		String ip = getRemoteAddress(request);
		//从fromBean里面得到新用户
		User user = userInfo.getUser();
		user.setId(getUuId());
		user.setCompanyId(company.getCompanyId());
		user.setCompanyName(company.getCompanyName());
		
		//密码sha加密
		user.setLoginPassword( EncrypSHA.eccrypt ( userInfo.getLoginPassword() ) );
		
		//新建root组
		Group  group =  new Group(getUuId(), company.getCompanyId(), company.getCompanyName(), 
				"root", Calendar.getInstance(), (byte)1);
		
		//新建公司的日志
		Log compLog = new Log(getUuId(), company.getCompanyId(), company.getCompanyName(),
				user.getId(), user.getLoginName(),(byte)22,
				Calendar.getInstance().getTimeInMillis(), ip);
		//保存用户日志
		Log userLog = new Log(getUuId(), company.getCompanyId(), company.getCompanyName(),
				user.getId(), user.getLoginName(),(byte)2, 
				Calendar.getInstance().getTimeInMillis(), ip);
				
		//生成保存组的日志
		Log groupLog = new Log(getUuId(), company.getCompanyId(), company.getCompanyName(),
				(byte)12, user.getId(), user.getLoginName(), 
				Calendar.getInstance().getTimeInMillis(), ip);
		
		//组和用户的多对多关系
		GroupsUsers gu = new GroupsUsers(company.getCompanyId(), 
				company.getCompanyName(), group.getId(), 
				group.getGroupName(), user.getId(), user.getLoginName());
		
		//初始化100G的容量
		long maxCapacity = getEntrpriseUserMaxCapacity(); 
		
		Capacity cap = new Capacity(getUuId(), company.getCompanyId(), company.getCompanyName(), 
				maxCapacity, 0, user.getId(), user.getLoginName());
		
		//保存新建空间日志
		Log capLog = new Log(getUuId(), company.getCompanyId(), company.getCompanyName(),
				 user.getId(), user.getLoginName(), (byte)42,
				Calendar.getInstance().getTimeInMillis(), ip);
		
		String storeName = request.getRealPath(getSaveDir());
		storeName = storeName + "/" + getUuId();
		
		FileSystem fileSystem = new FileSystem(getUuId(), company.getCompanyId(), company.getCompanyName(), 
				"..", storeName, (byte)1, "/", 0L, Calendar.getInstance().getTimeInMillis(), Calendar.getInstance().getTimeInMillis(), 
				user.getId(), user.getLoginName(), (byte)1, 777); 
		
		Log fileLog = new Log(getUuId(), company.getCompanyId(), company.getCompanyName(),
				 user.getId(), user.getLoginName(), (byte)32,
				Calendar.getInstance().getTimeInMillis(), ip);
		
		companyService.save(company, compLog, cap, capLog,
				group, groupLog, gu, user, userLog, fileSystem, fileLog);
		//标识改用户已经登录了
		session.setAttribute("logined", "enterprise");
		session.setAttribute("user", user);
		session.removeAttribute("company");
		return SUCCESS;
	}
}
